﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Triples {
    public class PlayThroughDeckController : Controller {

        public const int BoardSize = 12;
        private readonly Deck Deck;
        private readonly IPlayableCardFactory Factory;
        private readonly Board Board;

        public override Board GameBoard {
            get { return Board; }
        }


        public PlayThroughDeckController(IPlayableCardFactory factory) : base () {
            Deck = new Deck();
            Factory = factory;
            Board = GetStartingBoard();
        }


        protected Board GetStartingBoard() {
            Board board = new Board();

            for (int i = 0; i < BoardSize; i++) {
                board.PlaceCard(GetNextCard());
            }

            return board;
        }


        internal bool CanDealMoreCards() {
            return Deck.CanDraw();
        }


        public void DealMoreCards() {

            for (int i = 0; i < CardSet.SetSize; i++) {
                GameBoard.PlaceCard(GetNextCard());
            }
        }


        protected override bool ProcessFoundSet(CardSet set) {
            
            foreach (IPlayableCard c in set) {
                GameBoard.PickUpCard(c);

                if (Board.Size < BoardSize) {
                    IPlayableCard newCard = GetNextCard();

                    if (null != newCard) GameBoard.PlaceCard(newCard);
                    else GameBoard.CompactBoard();
                }

                else GameBoard.CompactBoard();
            }

            return true;
        }


        private IPlayableCard GetNextCard() {
            return GetNextCard(Deck);
        }

        public override ScoreCard ScoreBoard {
            get { throw new NotImplementedException(); }
        }
    }
}
